package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbCardBuyExample;
import com.bsb.base.model.BsbFirmExample;
import com.bsb.base.model.BsbUserExample;
import com.bsb.base.model.SysUser;
import com.bsb.base.model.UserFirm;
import com.bsb.base.model.BsbUserExample.Criteria;
import com.bsb.base.model.CardBuy;
import com.bsb.base.model.Invite;
import com.bsb.biz.service.BsbCardBuyService;
import com.bsb.biz.service.BsbUserService;
import com.bsb.biz.service.InviteService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/invite")
public class InviteController {
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/invite/list";
	
	@Resource
	private InviteService inviteService;
	@Resource
	private BsbUserService bsbUserService;
	@Resource
	private BsbCardBuyService bsbCardBuyService;
	
	@RequiresPermissions("invite:view")
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId==null){
			model.addAttribute("msg", "账号异常请联系管理员！");
			return "/invite/list";
		}
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria createCriteria = bsbUserExample.createCriteria();
		
		String username = (String) params.get("username");
		String supername = (String) params.get("supername");
		String firmname = (String) params.get("firmname");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		if(StringUtils.isEmpty(supername)){
			supername = null;
		}
		if(StringUtils.isEmpty(firmname)){
			firmname = null;
		}
		if(StringUtils.isNotBlank(username)){
			createCriteria.andNameLike("%"+username+"%");
		}
		
		if(startDate!=null){
			createCriteria.andRegistTimeGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			createCriteria.andRegistTimeLessThanOrEqualTo(endDate);
		}
		
		if(firmId!=10000){
			createCriteria.andFirmIdEqualTo(firmId.intValue());
		}
		createCriteria.andDelFlagEqualTo(Global.SHOW);
		int totalCount = inviteService.getCountInvite(firmname,supername, bsbUserExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbUserExample.setPageIndex((pageNum - 1) * pageSize);
		bsbUserExample.setPageSize(pageSize);
		bsbUserExample.setOrderByClause("bsb_user.registTime desc");
		List<Invite> inviteUsers = inviteService.getInvites(firmname,supername, bsbUserExample);
		for (Invite invite : inviteUsers) {
			String uuid = invite.getUuid();
			BsbUserExample userExample = new BsbUserExample();
			Criteria criteria = userExample.createCriteria();
			criteria.andInviteByEqualTo(uuid);
			int firstInviteNum = bsbUserService.countBsbUser(userExample );
			
			BsbCardBuyExample bsbCardBuyExample = new BsbCardBuyExample();
			com.bsb.base.model.BsbCardBuyExample.Criteria criteria1 = bsbCardBuyExample.createCriteria();
			criteria1.andUserIdEqualTo(uuid);
			criteria1.andIsPayEqualTo("0");
			int fstCardBuyNum = inviteService.fstCardBuyNum(uuid);
			
			int secondInviteNum = inviteService.secondInviteNum(uuid);
			int secondCardBuyNum = inviteService.secondCardBuyNum(uuid);
			Double totalMoney = inviteService.totalMoney(uuid)==null?0.0:inviteService.totalMoney(uuid);
			invite.setFirstInviteNum(firstInviteNum);
			invite.setFirstBuyCardNum(fstCardBuyNum);
			invite.setSecondInviteNum(secondInviteNum);
			invite.setSecondBuyCardNum(secondCardBuyNum);
			invite.setMoney(totalMoney);
		}
		model.addAttribute("username", username);
		model.addAttribute("supername", supername);
		model.addAttribute("firmname", firmname);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbUsers", inviteUsers);
		return "/invite/list";
	}
	
	
	@RequiresPermissions("invite:view")
	@RequestMapping(value="/fstinvites",method=RequestMethod.GET)
	public String fstinvites(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria criteria = bsbUserExample.createCriteria();
		
		String username = (String) params.get("username");
		String firmname = (String) params.get("firmname");
		String vip = (String) params.get("vip");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		if(StringUtils.isNotBlank(username)){
			criteria.andNameLike("%"+username+"%");
		}
		if(StringUtils.isNotEmpty(vip)){
			criteria.andVipEqualTo(vip);
		}
		if(startDate!=null){
			criteria.andRegistTimeGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			criteria.andRegistTimeLessThanOrEqualTo(endDate);
		}
		criteria.andInviteByEqualTo(uuid);
		
		BsbFirmExample firmExample = new BsbFirmExample();
		com.bsb.base.model.BsbFirmExample.Criteria createCriteria = firmExample.createCriteria();
		if(StringUtils.isNotEmpty(firmname)){
			createCriteria.andNameLike("%"+firmname+"%");
		}
		int totalCount = inviteService.countFstInvites(bsbUserExample,firmExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbUserExample.setPageIndex((pageNum - 1) * pageSize);
		bsbUserExample.setPageSize(pageSize);
		bsbUserExample.setOrderByClause("bsb_user.registTime desc");
		List<UserFirm> fstInvites = inviteService.getFstInvites(bsbUserExample,firmExample);
		model.addAttribute("username", username);
		model.addAttribute("vip", vip);
		model.addAttribute("firmname", firmname);
		model.addAttribute("uuid", uuid);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbUsers", fstInvites);
		return "/invite/invite";
	}
	
	@RequiresPermissions("invite:view")
	@RequestMapping(value="/sndinvites",method=RequestMethod.GET)
	public String sndinvites(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria criteria = bsbUserExample.createCriteria();
		
		String username = (String) params.get("username");
		String firmname = (String) params.get("firmname");
		String vip = (String) params.get("vip");
		String supername = (String) params.get("supername");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		if(StringUtils.isNotBlank(username)){
			criteria.andNameLike("%"+username+"%");
		}
		if(StringUtils.isNotEmpty(vip)){
			criteria.andVipEqualTo(vip);
		}
		if(startDate!=null){
			criteria.andRegistTimeGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			criteria.andRegistTimeLessThanOrEqualTo(endDate);
		}
		criteria.andInviteByEqualTo(uuid);
		
		BsbFirmExample firmExample = new BsbFirmExample();
		com.bsb.base.model.BsbFirmExample.Criteria createCriteria = firmExample.createCriteria();
		if(StringUtils.isNotEmpty(firmname)){
			createCriteria.andNameLike("%"+firmname+"%");
		}
		
		int totalCount = inviteService.count2ndInvites(bsbUserExample,firmExample,supername);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbUserExample.setPageIndex((pageNum - 1) * pageSize);
		bsbUserExample.setPageSize(pageSize);
		bsbUserExample.setOrderByClause("bsb_user.registTime desc");
		List<Invite> sndInvites = inviteService.get2ndInvites(bsbUserExample,firmExample,supername);
		model.addAttribute("username", username);
		model.addAttribute("vip", vip);
		model.addAttribute("firmname", firmname);
		model.addAttribute("supername", supername);
		model.addAttribute("uuid", uuid);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbUsers", sndInvites);
		return "/invite/sndinvite";
	}
	
	@RequiresPermissions("invite:view")
	@RequestMapping(value="/fstcardbuy",method=RequestMethod.GET)
	public String fstcardbuy(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria criteria = bsbUserExample.createCriteria();
		
		String username = (String) params.get("username");
		String firmname = (String) params.get("firmname");
		String ispay = (String) params.get("ispay");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		if(StringUtils.isNotBlank(username)){
			criteria.andNameLike("%"+username+"%");
		}
		BsbCardBuyExample cardBuyExample = new BsbCardBuyExample();
		com.bsb.base.model.BsbCardBuyExample.Criteria crriteria = cardBuyExample.createCriteria();
		if(StringUtils.isNotEmpty(ispay)){
			crriteria.andIsPayEqualTo(ispay);
		}
		if(startDate!=null){
			crriteria.andPaymentDateGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			crriteria.andPaymentDateLessThanOrEqualTo(endDate);
		}
		criteria.andInviteByEqualTo(uuid);
		
		BsbFirmExample firmExample = new BsbFirmExample();
		com.bsb.base.model.BsbFirmExample.Criteria createCriteria = firmExample.createCriteria();
		if(StringUtils.isNotEmpty(firmname)){
			createCriteria.andNameLike("%"+firmname+"%");
		}
		
		int totalCount = inviteService.count1stCardBuy(bsbUserExample,firmExample,cardBuyExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		cardBuyExample.setPageIndex((pageNum - 1) * pageSize);
		cardBuyExample.setPageSize(pageSize);
		cardBuyExample.setOrderByClause("bsb_card_buy.paymentDate desc");
		List<CardBuy> fstCardBuys = inviteService.get1stCardBuy(bsbUserExample, firmExample, cardBuyExample);
		model.addAttribute("username", username);
		model.addAttribute("ispay", ispay);
		model.addAttribute("firmname", firmname);
		model.addAttribute("uuid", uuid);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("fstCardBuys", fstCardBuys);
		return "/invite/fstcardbuy";
	}
	
	@RequiresPermissions("invite:view")
	@RequestMapping(value="/sndcardbuy",method=RequestMethod.GET)
	public String sndcardbuy(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbUserExample bsbUserExample = new BsbUserExample();
		Criteria criteria = bsbUserExample.createCriteria();
		
		String username = (String) params.get("username");
		String firmname = (String) params.get("firmname");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		if(StringUtils.isNotBlank(username)){
			criteria.andNameLike("%"+username+"%");
		}
		BsbCardBuyExample cardBuyExample = new BsbCardBuyExample();
		com.bsb.base.model.BsbCardBuyExample.Criteria crriteria = cardBuyExample.createCriteria();
		if(startDate!=null){
			crriteria.andPaymentDateGreaterThanOrEqualTo(startDate);
		}
		if(endDate!=null){
			endDate=DateUtils.addDays(endDate, 1);
			crriteria.andPaymentDateLessThanOrEqualTo(endDate);
		}
		
		BsbFirmExample firmExample = new BsbFirmExample();
		com.bsb.base.model.BsbFirmExample.Criteria createCriteria = firmExample.createCriteria();
		if(StringUtils.isNotEmpty(firmname)){
			createCriteria.andNameLike("%"+firmname+"%");
		}
		
		int totalCount = inviteService.count2ndCardBuy(bsbUserExample,firmExample,cardBuyExample,uuid);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		cardBuyExample.setPageIndex((pageNum - 1) * pageSize);
		cardBuyExample.setPageSize(pageSize);
		cardBuyExample.setOrderByClause("bsb_card_buy.paymentDate desc");
		List<CardBuy> sndCardBuys = inviteService.get2ndCardBuy(bsbUserExample, firmExample, cardBuyExample,uuid);
		model.addAttribute("username", username);
		model.addAttribute("firmname", firmname);
		model.addAttribute("uuid", uuid);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("sndCardBuys", sndCardBuys);
		return "/invite/sndcardbuy";
	}
}
